<?php
/**
 * 模板管理
 */

namespace backend\controllers;

use common\models\Config;
use common\models\Module;
use common\models\TemplateMap;
use yii;
use common\utils\XFile;

class TemplateController extends BaseController
{
    public function actionPost()
    {
        $files = XFile::getPost();
        foreach ((array)$files as $key => $item) {
            if ($map = TemplateMap::findOne(['template_name' => 'post&' . $item['basename']])) {
                $files[$key]['layout'] = $map->layout_name;
            } else {
                $files[$key]['layout'] = '';
            }
        }
        return $this->render('post', ['files' => $files]);
    }

    public function actionPostCreate()
    {
        $map = new TemplateMap();
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            $description = Yii::$app->request->post('description');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            if (XFile::existsPost($name)) {
                $this->tips("该文件名已经存在！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::savePost($name, $description, $data)) {
                $map->template_name = 'post&' . $name;
                $map->layout_name = Yii::$app->request->post('layout_name');
                $map->save();
                $this->redirect(['post'], 301);
            }
        }
        $layouts = XFile::getLayout();
        $layoutItems = [];
        foreach ((array)$layouts as $layout) {
            $layoutItems += [$layout['basename'] => $layout['name']];
        }
        $contents = ['description' => '', 'contents' => ''];
        return $this->xrender('post-form', ['map' => $map, 'layoutItems' => $layoutItems, 'name' => '', 'contents' => $contents]);
    }

    public function actionPostUpdate($id)
    {
        $contents = XFile::getPostContent($id);
        $id = str_replace('.php', '', $id);
        $map = TemplateMap::findOne(['template_name' => 'post&' . $id]);
        if (Yii::$app->request->post()) {
            $description = Yii::$app->request->post('description');
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::savePost($id, $description, $data)) {
                if (!$map) {
                    $map = new TemplateMap();
                }
                $map->template_name = 'post&' . $id;
                $map->layout_name = Yii::$app->request->post('layout_name');
                $map->save();
                $this->redirect(['post'], 301);
            }
        }
        $layouts = XFile::getLayout();
        $layoutItems = [];
        foreach ((array)$layouts as $layout) {
            $layoutItems += [$layout['basename'] => $layout['name']];
        }
        return $this->xrender('post-form', ['map' => $map, 'layoutItems' => $layoutItems, 'name' => $id, 'contents' => $contents]);
    }

    public function actionPostDelete($id)
    {
        if (XFile::deletePost($id)) {
            $id = str_replace('.php', '', $id);
            TemplateMap::deleteAll("template_name=:template_name", [':template_name' => 'post&' . $id]);
            $this->redirect(['post'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    public function actionCatalog()
    {
        $files = XFile::getCatalog();
        foreach ((array)$files as $key => $item) {
            if ($map = TemplateMap::findOne(['template_name' => 'catalog&' . $item['basename']])) {
                $files[$key]['layout'] = $map->layout_name;
            } else {
                $files[$key]['layout'] = '';
            }
        }
        return $this->render('catalog', ['files' => $files]);
    }

    public function actionCatalogCreate()
    {
        $map = new TemplateMap();
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            $description = Yii::$app->request->post('description');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            if (XFile::existsCatalog($name)) {
                $this->tips("该文件名已经存在！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::saveCatalog($name, $description, $data)) {
                $map->template_name = 'catalog&' . $name;
                $map->layout_name = Yii::$app->request->post('layout_name');
                $map->save();
                $this->redirect(['catalog'], 301);
            }
        }
        $layouts = XFile::getLayout();
        $layoutItems = [];
        foreach ((array)$layouts as $layout) {
            $layoutItems += [$layout['basename'] => $layout['name']];
        }
        $contents = ['description' => '', 'contents' => ''];
        return $this->xrender('catalog-form', ['map' => $map, 'layoutItems' => $layoutItems, 'name' => '', 'contents' => $contents]);
    }

    public function actionCatalogUpdate($id)
    {
        $contents = XFile::getCatalogContent($id);
        $id = str_replace('.php', '', $id);
        $map = TemplateMap::findOne(['template_name' => 'catalog&' . $id]);
        if (Yii::$app->request->post()) {
            $description = Yii::$app->request->post('description');
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::saveCatalog($id, $description, $data)) {
                if (!$map) {
                    $map = new TemplateMap();
                }
                $map->template_name = 'catalog&' . $id;
                $map->layout_name = Yii::$app->request->post('layout_name');
                $map->save();
                $this->redirect(['catalog'], 301);
            }
        }
        $layouts = XFile::getLayout();
        $layoutItems = [];
        foreach ((array)$layouts as $layout) {
            $layoutItems += [$layout['basename'] => $layout['name']];
        }
        return $this->xrender('catalog-form', ['map' => $map, 'layoutItems' => $layoutItems, 'name' => $id, 'contents' => $contents]);
    }

    public function actionCatalogDelete($id)
    {
        if (XFile::deleteCatalog($id)) {
            $id = str_replace('.php', '', $id);
            TemplateMap::deleteAll("template_name=:template_name", [':template_name' => 'catalog&' . $id]);
            $this->redirect(['catalog'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    public function actionHome()
    {
        $map = TemplateMap::findOne(['template_name' => 'site&index']);
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post('contents');
            XFile::saveHome($data);
            if (!$map) {
                $map = new TemplateMap();
            }
            $map->template_name = 'site&index';
            $map->layout_name = Yii::$app->request->post('layout_name');
            $map->save();
        }
        $contents = XFile::getHomeContent();
        $layouts = XFile::getLayout();
        $layoutItems = [];
        foreach ((array)$layouts as $layout) {
            $layoutItems += [$layout['basename'] => $layout['name']];
        }
        return $this->xrender('home', ['map' => $map, 'layoutItems' => $layoutItems, 'contents' => $contents]);
    }

    public function actionLayout()
    {
        $files = XFile::getLayout();
        return $this->render('layout', ['files' => $files]);
    }

    public function actionLayoutCreate()
    {
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            $description = Yii::$app->request->post('description');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::createLayout($name, $description, $data)) {
                $this->redirect(['layout'], 301);
            }
        }
        return $this->xrender('layout-form', ['name' => '', 'contents' => '']);
    }

    public function actionLayoutUpdate($id)
    {
        $contents = XFile::getLayoutContent($id);
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post('contents');
            if (XFile::saveLayout($id, $data)) {
                $this->redirect(['layout'], 301);
            }
        }
        return $this->xrender('layout-form', ['name' => $id, 'contents' => $contents]);
    }

    public function actionLayoutDelete($id)
    {
        if (XFile::deleteLayout($id)) {
            $this->redirect(['layout'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    public function actionItem()
    {
        $files = XFile::getItem();
        return $this->render('item', ['files' => $files]);
    }

    public function actionItemCreate()
    {
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            $description = Yii::$app->request->post('description');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            if (XFile::existsItem($name)) {
                $this->tips("该文件名已经存在！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::saveItem($name, $description, $data)) {
                $this->redirect(['item'], 301);
            }
        }
        $contents = ['description' => '', 'contents' => ''];
        return $this->render('item-form', ['name' => '', 'contents' => $contents]);
    }

    public function actionItemUpdate($id)
    {
        $contents = XFile::getItemContent($id);
        $id = str_replace('.php', '', $id);
        if (Yii::$app->request->post()) {
            $description = Yii::$app->request->post('description');
            if (!$description) {
                $this->tips("描述不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XFile::saveItem($id, $description, $data)) {
                $this->redirect(['item'], 301);
            }
        }
        return $this->render('item-form', ['name' => $id, 'contents' => $contents]);
    }

    public function actionItemDelete($id)
    {
        if (XFile::deleteItem($id)) {
            $this->redirect(['item'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    private function xrender($view, $parames = [])
    {
        $configs = Config::find()->all();
        $modules = Module::find()->all();
        return $this->render($view, array_merge($parames, ['configs' => $configs, 'modules' => $modules]));
    }
}